package cn.gailvlun.gll.presentation.chat;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;

import com.github.markzhai.recyclerview.BaseViewAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.chatlib.event.FriendEvent;
import cn.gailvlun.gll.databinding.ActivityFriendListBinding;
import cn.gailvlun.gll.net.HttpMethods;
import cn.gailvlun.gll.net.HttpSubscriber;
import cn.gailvlun.gll.net.ListRes;
import cn.gailvlun.gll.net.QueryParam;
import cn.gailvlun.gll.net.base.Profile;
import cn.gailvlun.gll.presentation.base.BaseActivity;
import cn.gailvlun.gll.presentation.chat.adapter.FriendListAdapter;
import cn.gailvlun.gll.presentation.profile.OtherProfileActivity;
import cn.gailvlun.gll.util.DialogUtil;
import cn.gailvlun.gll.util.IsEmpty;
import cn.gailvlun.gll.util.RxUtil;
import cn.gailvlun.gll.widget.NavigationBarView;
import cn.gailvlun.gll.widget.statelayout.State;



public class FriendListActivity extends BaseActivity implements OnRefreshListener, OnLoadmoreListener {

    public static void openActivity(Context context) {
        Intent intent = new Intent(context, FriendListActivity.class);
        context.startActivity(intent);
    }

    private ActivityFriendListBinding mBinding;
    private FriendListAdapter mAdapter;
    private LinearLayoutManager mLayoutManager;

    private QueryParam mQueryParam = new QueryParam();

    @Override
    protected void initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_friend_list);
        mBinding.setPresenter(new Presenter());

        mAdapter = new FriendListAdapter(mContext);
        mAdapter.setPresenter(new Presenter());
        mBinding.recyclerView.setAdapter(mAdapter);

        mLayoutManager = new LinearLayoutManager(mContext);
        mBinding.recyclerView.setLayoutManager(mLayoutManager);

        mBinding.srl.setOnRefreshListener(this);
        mBinding.srl.setOnLoadmoreListener(this);
        mBinding.srl.setEnableRefresh(false);
        mBinding.srl.setEnableLoadmore(false);

        mBinding.stateLayout.show(State.EMPTY);
    }

    @Override
    protected void createHandler() {
        mBinding.nbv.setOnBackListener(new NavigationBarView.OnBackListener() {
            @Override
            public void onBack(NavigationBarView nb) {
                onBackPressed();
            }
        });
        mBinding.nbv.setOnMenuClickListener(new NavigationBarView.OnMenuClickListener() {
            @Override
            public void onMenuClick(NavigationBarView nb) {
                AddFriendActivity.openActivity(mContext);
            }
        });
    }

    @Override
    protected void loadData(@Nullable Bundle savedInstanceState) {
    }

    @Override
    protected void onStart() {
        super.onStart();

        loadFriendsFromDb();
    }

    public void loadFriendsFromDb() {

        HttpMethods.getFriendService()
                .getFriends(mQueryParam.getPage())
                .compose(RxUtil.<ListRes<Profile>>applyScheduler())
                .compose(this.<ListRes<Profile>>bindToLifecycle())
                .subscribe(new HttpSubscriber<ListRes<Profile>>() {
                    @Override
                    protected void onSuccess(ListRes<Profile> response) {
                        loadRecyclerView(response.getResults(), true, response.isNext());
                    }

                    @Override
                    protected void onFailure(String errMsg, @Nullable ListRes<Profile> response, int code) {
                        DialogUtil.showError(mContext, errMsg);
                    }
                });
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        mQueryParam.resetPage();
        HttpMethods.getFriendService().getFriends(mQueryParam.getPage())
                .compose(RxUtil.<ListRes<Profile>>applyScheduler())
                .compose(this.<ListRes<Profile>>bindToLifecycle())
                .subscribe(new HttpSubscriber<ListRes<Profile>>() {
                    @Override
                    protected void onSuccess(ListRes<Profile> response) {
                        mBinding.srl.finishRefresh();

                        loadRecyclerView(response.getResults(), true, response.isNext());
                    }

                    @Override
                    protected void onFailure(String errMsg, @Nullable ListRes<Profile> response, int code) {
                        mBinding.srl.finishRefresh();
                    }
                });
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        mQueryParam.nextPage();
        HttpMethods.getFriendService().getFriends(mQueryParam.getPage())
                .compose(RxUtil.<ListRes<Profile>>applyScheduler())
                .compose(this.<ListRes<Profile>>bindToLifecycle())
                .subscribe(new HttpSubscriber<ListRes<Profile>>() {
                    @Override
                    protected void onSuccess(ListRes<Profile> response) {
                        mBinding.srl.finishLoadmore();

                        loadRecyclerView(response.getResults(), true, response.isNext());
                    }

                    @Override
                    protected void onFailure(String errMsg, @Nullable ListRes<Profile> response, int code) {
                        mBinding.srl.finishLoadmore();
                    }
                });
    }

    private void loadRecyclerView(List<Profile> profiles, boolean isRefresh, boolean isMore) {
        if (isRefresh) {
            if (IsEmpty.list(profiles)) {
                mAdapter.clear();
                mBinding.stateLayout.show(State.EMPTY);
            } else {
                mBinding.stateLayout.show(State.CONTENT);
                mAdapter.set(profiles);
            }
        } else {
            mAdapter.addAll(profiles);
        }

        mBinding.srl.setEnableLoadmore(isMore);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onFriendEvent(FriendEvent friendEvent) {
        if (friendEvent != null) {
            loadFriendsFromDb();
        }
    }

    public class Presenter implements BaseViewAdapter.Presenter {

        public void onClickProfile(Profile item) {
            OtherProfileActivity.openActivity(mContext, item, true);
        }
    }
}
